<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//登录控制器
class LoginController extends Controller{
	
	//登录
	public function index(){
		if(IS_POST){
			//接收post过来的用户名和密码
			$uname = Q('post.uname');
			//注意密码默认要加密
			$upassword = Q('post.upassword','','md5');
			//用得到的用户名和密码和数据库里的user表匹配
			//select * from user where {$uname} and {$upassword};
			$userData = Db::table('user')->where("uname='{$uname}' AND upassword='{$upassword}'")->get();
//			sp($userData);exit;
			//如果匹配的话返回真，不成功返回假
			if($userData){
				$_SESSION['userid'] = $userData[0]['userid'];
//				sp($userid);
				$_SESSION['uname'] = $userData[0]['uname'];
				View::success('登录成功',U('Index/index'));
			}else{
				View::error('用户名或密码错误');
			}
		}
		View::make();
	}
	
	
	//显示验证码
	public function code(){
		Code::width(140)->height(40)->fontSize(24)->fontColor('#f00f00')->make();
	}
	//异步判断验证码是否正确
	public function codes(){
	    $code = Q('post.code');
		if($code == ''){
			echo 1;exit;
		}
		
		//strtoupper() 函数把字符串转换为大写
		//Code::get();是框架里的方法“获取验证码文字”
		if(strtoupper($code) == Code::get()){
			echo 0;exit;
		}else{
			echo 2;exit;
		}
	}
	
	//退出登录
	public function out(){
		unset($_SESSION['userid']);
		unset($_SESSION['uname']);
		go('index/index');
	}
} 

 ?>